package com.inossem.warehousestorage_inossem.activity.allocation.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.extra.AllocationInStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationInBatchDetailsAdapter;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInMatsBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;

import java.util.List;

/**
 * TODO
 *
 * @author 詹建宇
 * @time on 2018/4/8 10:14
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInBatchDetailsActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private PullableListView listView;

    private String mat_id;
    private AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean;//物料
    private List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> stockPositionList;//批次列表

    private AllocationInBatchDetailsAdapter adapter;

    private String condition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_in_storage_allocation_listview);

        initView(this);
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        getExtra(this);
        initData(this);
        initOper(this);
    }

    private void getExtra(Activity activity) {
        mat_id = activity.getIntent().getStringExtra(AllocationInStorageExtra.MAT_ID);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        listView = (PullableListView) activity.findViewById(R.id.listView);

        listView.setPullUpFlag(false);
        listView.setPullDownFlag(false);
    }

    @Override
    public void initData(Activity activity) {
        title.setText("物料明细");
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String result = check();
                if (TextUtils.isEmpty(result)) {
                    goDie();
                } else {
                    DialogUtils.getConfirmDialog(activity, result, new DialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onConfirm(Dialog dialog) {
                            dialog.dismiss();
                            goDie();
                        }
                    }).show();
                }
            }
        });
        condition = "";
        itemListBean = AllocationUtils.getItemListBeanByMatId(activity, mat_id);
        stockPositionList = itemListBean.getStock_position_list();

        addListData(activity, stockPositionList, listView);
    }

    private void addListData(Activity activity, List<AllocationInMatsBean.MatsBodyBean.ItemListBean.StockPositionListBean> list, ListView listView) {
        adapter = new AllocationInBatchDetailsAdapter(activity, condition, itemListBean, list);
        listView.setAdapter(adapter);
    }

    private void initOper(Activity activity) {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                i--;
                Intent intent = new Intent(activity, AllocationInPositionsActivity.class);
                intent.putExtra(AllocationInStorageExtra.MAT_ID, mat_id);
                intent.putExtra(AllocationInStorageExtra.BATCH, stockPositionList.get(i).getBatch());
                activity.startActivity(intent);
            }
        });
    }

    @Override
    public void onBackPressed() {
        String result = check();
        if (TextUtils.isEmpty(result)) {
            goDie();
        } else {
            DialogUtils.getConfirmDialog(this, result, new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    dialog.dismiss();
                    goDie();
                }
            }).show();
        }
    }

    private String check() {
        String msg = null;
        String inNum = adapter.getInNum();
        String outNum = adapter.getOutNum();
        if (!inNum.equals(outNum)) {
            msg = "出库数量与入库数量不相等,确认退出?";
        }
        return msg;
    }

    private void goDie() {
        AllocationUtils.saveIsOkThisMatOfBatch(this, mat_id, adapter.getInNum(), adapter.getOutNum());
        finish();
    }
}
